<template>
    <div class="WorkScope">
        <aside-page @getOrganization="getOrganization"></aside-page>
        <div class="left">
            <div class="search">
                <span>地区关键词：</span>
                <el-input v-model="keyword" placeholder="请输入地区关键词" style="width: 200px"></el-input>
            </div>
            <div class="map" ref="map">
            </div>
        </div>
    </div>
</template>

<script>
import AsidePage from '@/components/Aside.vue'

export default {
    name: 'WorkScope',
    components: { AsidePage },
    mounted () {
        this.$store.commit('setPageLoading', false)
    },
    data () {
        return {
            map: '',
            point: '',
            marker: '',
            keyword: '',
            local: ''
        }
    },
    methods: {
        getOrganization (data) {
            this.point = {
                lat: data.latitude,
                lng: data.longitude
            }
            this.renderMap()
        },
        renderMap () {
            this.map = new window.BMapGL.Map(this.$refs.map)
            this.map.centerAndZoom(this.point, 15)
            this.map.enableScrollWheelZoom(true)

            const localCtrl = new window.BMapGL.LocationControl()
            this.map.addControl(localCtrl)

            //添加一个点
            this.marker = new window.BMapGL.Marker(this.point) // 创建标注
            this.map.addOverlay(this.marker) // 将标注添加到地图中
            this.local = new window.BMapGL.LocalSearch(this.map, {
                renderOptions: { map: this.map }
            })
        }
    },
    watch: {
        keyword: {
            handler () {
                if (this.keyword === '') {
                    this.local.clearResults()
                    this.map.centerAndZoom(this.point, 15)
                } else {
                    this.local.search(this.keyword)
                }
            }
        }
    }
}
</script>

<style scoped lang="scss">
.WorkScope {
    display: flex;
    overflow: auto;
    height: 100%;

    .left {
        flex: 1;
        background-color: white;
        border-radius: 5px;
        color: #2A2929;
        margin: 12px;
        padding: 24px;
        display: flex;
        flex-direction: column;

        .search {
            display: flex;
            align-items: center;
            margin-bottom: 24px;

            span {
                font-size: 14px;
            }
        }

        .map {
            min-width: 800px;
            min-height: 600px;
            flex: 1;
            border-radius: 5px;
        }
    }
}
</style>
